import { defineStore } from "pinia"
import { findMenuByName } from "@/apis/menuApi"
//定义好了一个仓库对象。
//pinia状态机内部只有三个模块
//state：仓库数据
//getters：仓库计算属性
//actions：仓库行为
export const userStore = defineStore("userStore", {
    state: () => {
        return {
            username: "",
            password: "123",
            age: 23,
            role: ""
        }
    },
    getters: {
        fullName(state) {
            return state.username.toUpperCase()
        }
    },
    actions: {
        increment(age: number) {
            this.age = age
        },
        async increment2() {
            const res = await findMenuByName("bobo")
            console.log("menus", res);
        },
        changeStoreUsername(username:string){
            this.username = username
        },
        changeStoreRolename(roleName:string){
            this.role = roleName
        }
    },
    persist:{
        //开启持久化
        enabled:true, 
        //数据存储的策略
        strategies:[
            {
                key:"user",
                storage:sessionStorage,
                paths:["username","role"]
            }
        ] 
    }
})